package org.i_plus.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i_plus.Assistance;
import org.i_plus.model.MemberBean;
import org.i_plus.model.services.FriendService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ChatServlet extends HttpServlet {
	private FriendService service;	
	@Override
	public void init() throws ServletException {
		WebApplicationContext context =
				WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
		this.service = (FriendService)context.getBean("friendService");
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String fromacc = req.getParameter("fromacc");
		List<MemberBean> friendlist = service.friendList(fromacc);
		List<String> account = new ArrayList<String>();
		List<String> pic = new ArrayList<String>();
		for(MemberBean temp:friendlist){
		account.add(temp.getAccount());
		pic.add(Assistance.photoByteToImgString("image/jpeg", temp.getPic1()));
		}			
		req.setAttribute("friendlist", account);
		req.setAttribute("pic", pic);
		
		req.getRequestDispatcher("/View/Chat.jsp").forward(req, resp);
		return;
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}
	
}
